/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bitcar.platform.system.action;

import com.bitcar.common.constvalues.ConstValues;
import com.bitcar.platform.system.model.Mailconfirm;
import com.bitcar.platform.system.model.User;
import com.bitcar.platform.system.service.SystemService;
import com.haoduocan.util.Utils;
import java.net.URLEncoder;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 *
 * @author shd
 */
@Controller
@RequestMapping("/System")
@SessionAttributes("user")
public class SystemController {

    private final Log log = LogFactory.getLog(SystemController.class);
    private SystemService systemService;

    @Autowired
    public void setSystemService(SystemService systemService) {
        this.systemService = systemService;
    }
    
    public SystemController(){
        log.info("inti systemcontroller");
    }

    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, ModelMap model) {

        try {
            User user = systemService.getUser(username, password);
            if (user != null) {

                model.addAttribute("user", user);
                return "redirect:/System/toFrame.do";
            } else {
                return "system/nologin.jsp";
            }
        } catch (Exception ex) {
            log.error("login", ex);
             return ConstValues.COMMONEXCEPTION_KEY;
        }
    }

    @RequestMapping(value = "/activeAccount.do", method = RequestMethod.POST)
    public String activeAccount(@RequestParam("usercode") String usercode, ModelMap model, HttpServletRequest hreq) {

        try {
            User u = systemService.getUser(usercode);
            if (u != null) {

                String md5 = Utils.convertMD5(Utils.dateToStr(new Date()) + u.getId());
                StringBuilder sb = new StringBuilder();
                sb.append(hreq.getScheme()).append("://").append(hreq.getServerName()).append(":").append(hreq.getServerPort());
                sb.append(hreq.getContextPath()).append("/System/confirmVerifyCode.do?id=").append(u.getId()).append("&code=")
                        .append(md5).append("&email=").append(URLEncoder.encode(u.getEmail(), "utf-8"));

                Mailconfirm mc = new Mailconfirm();
                mc.setUserid(u.getId());
                mc.setVerifyurl(sb.toString());
                mc.setExpiretime(DateUtils.addHours(new Date(), 4));    //失效时间设置为4小时
                mc.setEmail(u.getEmail());
                mc.setSend(false);
                mc.setVerifycode(md5);
                systemService.saveMailconfirm(mc);

                return "redirect:/index.html";
            } else {
                return "";
            }
        } catch (Exception ex) {
            log.error("active", ex);
             return ConstValues.COMMONEXCEPTION_KEY;
        }
    }

    @RequestMapping(value = "/confirmVerifyCode.do", method = RequestMethod.GET)
    public String confirmVerifyCode(ModelMap model, @RequestParam("email") String email, @RequestParam("id") String id, @RequestParam("code") String code) {
        try {
            Mailconfirm mc = systemService.getMailconfirm(email, code, id);
            if (mc != null) {
                model.addAttribute("userid", mc.getUserid());
                return "/system/resetpassword";
            } else {

                return "/system/active";
            }
        } catch (Exception ex) {
            return "";
        }
    }

    @RequestMapping(value = "/newAccount.do", method = RequestMethod.GET)
    public String newAccount(ModelMap model, HttpServletRequest hreq) {

        return "/system/active";
    }
    
    @RequestMapping(value="/resetPassword.do",method= RequestMethod.POST)
    public String resetPassword(@RequestParam("password") String password, 
    @RequestParam("repassword") String repassword, @RequestParam("userid") String userid, 
            @RequestParam("verifycode") String verifycode,HttpServletRequest hreq) {
        try{
        User u=systemService.getUser(NumberUtils.toInt(userid));
        if(StringUtils.equals(password, repassword)){
            u.setPassword(password);
            systemService.updateUser(u);
            
            systemService.delMailconfirm(u.getEmail(), verifycode, NumberUtils.toInt(userid));
            return "/index";
        }else{
            return "";
        }
        }catch(Exception ex){
            log.error("resetPassword",ex);
             return ConstValues.COMMONEXCEPTION_KEY;
        }
        
        
    }
    

    @RequestMapping(value = "/toFrame.do", method = RequestMethod.GET)
    public String toFrame(ModelMap model) {
        
        return "/system/frame";
    }
}
